unit oPrimitiveWorld;

interface
uses oCoordinates, oPhysicalEntity, Types, Math, oLogicalEntity;

  type
    // ancestor for cellular and real world
    TPrimitiveWorld = class(TInterfacedObject)
      protected
        aSectorColumnCount, aSectorRowCount : integer; // sector count on width/ height
        aSectorSize : real;   // sector size
        function findUpSector(paPoint: TCoordinates): TPoint;  // returns coordinates of a sector during searching of topleft border of obstacle
        function findDSector(paPoint: TCoordinates): TPoint;  // returns coordinates of a sector during searching of bottomright border of obstacle
        function findSector(paPoint: TCoordinates): TPoint;   // returns coordinates of a sector where paPoint is located
      public
        // creates instance and sets attributes
        constructor Create(paSectorSize: real; paSectorRowCount: integer; paSectorColumnCount: integer); overload;
    end;

implementation


constructor TPrimitiveWorld.Create(paSectorSize: real; paSectorRowCount,
  paSectorColumnCount: integer);
begin
  aSectorColumnCount := paSectorColumnCount;
  aSectorRowCount := paSectorRowCount;
  aSectorSize := paSectorSize;
end;

function TPrimitiveWorld.findDSector(paPoint: TCoordinates): TPoint;
var
   point : Tpoint;
begin
  point.X := round(paPoint.x/aSectorSize);
  if point.x >= aSectorColumnCount then
   point.X := aSectorColumnCount - 1;
   
  if point.x < 0 then point.x := 0;

  point.Y := floor(paPoint.y/aSectorSize);
  if point.y >= aSectorRowCount then
   point.y := aSectorRowCount - 1;
  if point.y < 0 then
   point.y := 0;
  Result := point;
end;

function TPrimitiveWorld.findSector(paPoint: TCoordinates): TPoint;
var
  point : Tpoint;
begin
  point.X := floor(paPoint.x/aSectorSize);
  if point.x >= aSectorColumnCount then
   point.X := -1;
  if point.x < 0 then point.x := -1;

  point.Y := floor(paPoint.y/aSectorSize);
  if point.y >= aSectorRowCount then
   point.y := -1;
  if point.y < 0 then
   point.y := -1;
  Result := point;
end;

function TPrimitiveWorld.findUpSector(paPoint: TCoordinates): TPoint;
var
  point : Tpoint;
begin
  point.X := floor(paPoint.x/aSectorSize);
  if point.x >= aSectorColumnCount then
   point.X := aSectorColumnCount - 1;
  if point.x < 0 then point.x := 0;

  point.Y := round(paPoint.y/aSectorSize);
  if point.y >= aSectorRowCount then
   point.y := aSectorRowCount - 1;
  if point.y < 0 then
   point.y := 0;
  Result := point;
end;

end.
 